package com.ruoyi.library.controller;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.library.domain.vo.NexusToolLibraryVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.library.domain.NexusToolLibrary;
import com.ruoyi.library.service.INexusToolLibraryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资源库Controller
 *
 * @author Afanda
 * @date 2025-04-29
 */
@RestController
@RequestMapping("/NexusSphere/library")
public class NexusToolLibraryController extends BaseController {
    @Autowired
    private INexusToolLibraryService nexusToolLibraryService;

    @Autowired
    private RedisCache redisCache;

    private static final String LIBRARY_CACHE_KEY = "library";


    /**
     * 查询资源库列表
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:library:list')")
    @GetMapping("/list")
    public TableDataInfo list(NexusToolLibrary nexusToolLibrary) {
        startPage();
        List<NexusToolLibraryVo> list = nexusToolLibraryService.selectNexusToolLibraryVoList(nexusToolLibrary);
        return getDataTable(list);
    }

    /**
     * 导出资源库列表
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:library:export')")
    @Log(title = "资源库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NexusToolLibrary nexusToolLibrary) {
        List<NexusToolLibrary> list = nexusToolLibraryService.selectNexusToolLibraryList(nexusToolLibrary);
        ExcelUtil<NexusToolLibrary> util = new ExcelUtil<NexusToolLibrary>(NexusToolLibrary.class);
        util.exportExcel(response, list, "资源库数据");
    }

    /**
     * 获取资源库详细信息
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:library:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(nexusToolLibraryService.selectNexusToolLibraryById(id));
    }

    /**
     * 新增资源库
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:library:add')")
    @Log(title = "资源库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NexusToolLibrary nexusToolLibrary) {
        Long categoryId = nexusToolLibrary.getCategoryId();
        String key = LIBRARY_CACHE_KEY + categoryId;
        redisCache.deleteObject(key);
        return toAjax(nexusToolLibraryService.insertNexusToolLibrary(nexusToolLibrary));
    }

    /**
     * 修改资源库
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:library:edit')")
    @Log(title = "资源库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NexusToolLibrary nexusToolLibrary) {
        Long categoryId = nexusToolLibrary.getCategoryId();
        String key = LIBRARY_CACHE_KEY + categoryId;
        redisCache.deleteObject(key);
        return toAjax(nexusToolLibraryService.updateNexusToolLibrary(nexusToolLibrary));
    }

    /**
     * 删除资源库
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:library:remove')")
    @Log(title = "资源库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nexusToolLibraryService.deleteNexusToolLibraryByIds(ids));
    }


}
